<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>

	
		<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
		<meta http-equiv="Content-Language" content="en-us">

		<title>Django | Request and response objects | Django Documentation</title>

		<meta name="ROBOTS" content="ALL">
		<meta http-equiv="imagetoolbar" content="no">
		<meta name="MSSmartTagsPreventParsing" content="true">
		<meta name="Copyright" content="This site's design and contents Copyright (c) 2005  Lawrence Journal-World.">

		<meta name="keywords" content="Python, Django, framework, open-source">
		<meta name="description" content="Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.">

		<link href="Django%20%7C%20Request%20and%20response%20objects%20%7C%20Django%20Documentation_files/base.css" rel="stylesheet" type="text/css" media="screen">
		<link href="Django%20%7C%20Request%20and%20response%20objects%20%7C%20Django%20Documentation_files/print.css" rel="stylesheet" type="text/css" media="print">
      
  
  <style type="text/css" media="screen">
    #docs-search {
      color: #000;
      float: right;
    }
    #docs-search form {
      font-size: 92%;
      margin: 0;
      padding: 1em 1em 0;
      white-space: nowrap;
    }
    form.search ul {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    form.search li {
      display: inline;
      padding-right: 1em;
    }
    form.search .query {
      width: 18em;
    }
  </style>
  <link rel="stylesheet" href="Django%20%7C%20Request%20and%20response%20objects%20%7C%20Django%20Documentation_files/pygments.css" type="text/css">

	</head><body id="documentation" class="default">

	<div id="container">
		<div id="header">
			<h1 id="logo"><a href="http://www.djangoproject.com/"><img src="Django%20%7C%20Request%20and%20response%20objects%20%7C%20Django%20Documentation_files/hdr_logo.gif" alt="Django"></a></h1>
			<ul id="nav-global">
				<li id="nav-homepage"><a href="http://www.djangoproject.com/">Home</a></li>
				<li id="nav-download"><a href="http://www.djangoproject.com/download/">Download</a></li>
				<li id="nav-documentation"><a href="http://docs.djangoproject.com/">Documentation</a></li>
				<li id="nav-weblog"><a href="http://www.djangoproject.com/weblog/">Weblog</a></li>
				<li id="nav-community"><a href="http://www.djangoproject.com/community/">Community</a></li>
				<li id="nav-code"><a href="http://code.djangoproject.com/">Code</a></li>
			</ul>
		</div>
		<!-- END Header -->
		<div id="billboard">
  <h2><a href="http://docs.djangoproject.com/en/1.0/">Django documentation</a></h2>
</div>
		<div id="columnwrap">
			
		<div id="content-main">
		


  <h2 class="deck">
  
    This document describes Django version 1.0. For development documentation, 
    <a href="http://docs.djangoproject.com/en/dev/ref/request-response/">go here</a>.
  
  </h2>
  <div class="section" id="s-module-django.http">
<span id="s-ref-request-response"></span><span id="module-django.http"></span><span id="ref-request-response"></span><h1>Request and response objects<a class="headerlink" href="#module-django.http" title="Permalink to this headline">¶</a></h1>
<div class="section" id="s-quick-overview">
<span id="quick-overview"></span><h2>Quick overview<a class="headerlink" href="#quick-overview" title="Permalink to this headline">¶</a></h2>
<p>Django uses request and response objects to pass state through the system.</p>
<p>When a page is requested, Django creates an <a title="django.http.HttpRequest" class="reference internal" href="#django.http.HttpRequest"><tt class="xref docutils literal"><span class="pre">HttpRequest</span></tt></a> object that
contains metadata about the request. Then Django loads the appropriate view,
passing the <a title="django.http.HttpRequest" class="reference internal" href="#django.http.HttpRequest"><tt class="xref docutils literal"><span class="pre">HttpRequest</span></tt></a> as the first argument to the view function.
Each view is responsible for returning an <a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a> object.</p>
<p>This document explains the APIs for <a title="django.http.HttpRequest" class="reference internal" href="#django.http.HttpRequest"><tt class="xref docutils literal"><span class="pre">HttpRequest</span></tt></a> and
<a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a> objects.</p>
</div>
<div class="section" id="s-httprequest-objects">
<span id="httprequest-objects"></span><h2>HttpRequest objects<a class="headerlink" href="#httprequest-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.http.HttpRequest">
<!--[django.http.HttpRequest]-->class <tt class="descname">HttpRequest</tt><a class="headerlink" href="#django.http.HttpRequest" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="section" id="s-attributes">
<span id="attributes"></span><h3>Attributes<a class="headerlink" href="#attributes" title="Permalink to this headline">¶</a></h3>
<p>All attributes except <tt class="docutils literal"><span class="pre">session</span></tt> should be considered read-only.</p>
<dl class="attribute">
<dt id="django.http.HttpRequest.path">
<!--[django.http.HttpRequest.path]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">path</tt><a class="headerlink" href="#django.http.HttpRequest.path" title="Permalink to this definition">¶</a></dt>
<dd><p>A string representing the full path to the requested page, not including
the domain.</p>
<p>Example: <tt class="docutils literal"><span class="pre">"/music/bands/the_beatles/"</span></tt></p>
</dd></dl>

<dl class="attribute">
<dt id="django.http.HttpRequest.method">
<!--[django.http.HttpRequest.method]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">method</tt><a class="headerlink" href="#django.http.HttpRequest.method" title="Permalink to this definition">¶</a></dt>
<dd><p>A string representing the HTTP method used in the request. This is
guaranteed to be uppercase. Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">'GET'</span><span class="p">:</span>
    <span class="n">do_something</span><span class="p">()</span>
<span class="k">elif</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">'POST'</span><span class="p">:</span>
    <span class="n">do_something_else</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.http.HttpRequest.encoding">
<!--[django.http.HttpRequest.encoding]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">encoding</tt><a class="headerlink" href="#django.http.HttpRequest.encoding" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>A string representing the current encoding used to decode form submission
data (or <tt class="xref docutils literal"><span class="pre">None</span></tt>, which means the <tt class="docutils literal"><span class="pre">DEFAULT_CHARSET</span></tt> setting is used).
You can write to this attribute to change the encoding used when accessing
the form data. Any subsequent attribute accesses (such as reading from
<tt class="docutils literal"><span class="pre">GET</span></tt> or <tt class="docutils literal"><span class="pre">POST</span></tt>) will use the new <tt class="docutils literal"><span class="pre">encoding</span></tt> value.  Useful if you
know the form data is not in the <tt class="docutils literal"><span class="pre">DEFAULT_CHARSET</span></tt> encoding.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.http.HttpRequest.GET">
<!--[django.http.HttpRequest.GET]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">GET</tt><a class="headerlink" href="#django.http.HttpRequest.GET" title="Permalink to this definition">¶</a></dt>
<dd>A dictionary-like object containing all given HTTP GET parameters. See the
<tt class="docutils literal"><span class="pre">QueryDict</span></tt> documentation below.</dd></dl>

<dl class="attribute">
<dt id="django.http.HttpRequest.POST">
<!--[django.http.HttpRequest.POST]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">POST</tt><a class="headerlink" href="#django.http.HttpRequest.POST" title="Permalink to this definition">¶</a></dt>
<dd><p>A dictionary-like object containing all given HTTP POST parameters. See the
<tt class="docutils literal"><span class="pre">QueryDict</span></tt> documentation below.</p>
<p>It's possible that a request can come in via POST with an empty <tt class="docutils literal"><span class="pre">POST</span></tt>
dictionary -- if, say, a form is requested via the POST HTTP method but
does not include form data. Therefore, you shouldn't use <tt class="docutils literal"><span class="pre">if</span> <span class="pre">request.POST</span></tt>
to check for use of the POST method; instead, use <tt class="docutils literal"><span class="pre">if</span> <span class="pre">request.method</span> <span class="pre">==</span>
<span class="pre">"POST"</span></tt> (see above).</p>
<p>Note: <tt class="docutils literal"><span class="pre">POST</span></tt> does <em>not</em> include file-upload information. See <tt class="docutils literal"><span class="pre">FILES</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.http.HttpRequest.REQUEST">
<!--[django.http.HttpRequest.REQUEST]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">REQUEST</tt><a class="headerlink" href="#django.http.HttpRequest.REQUEST" title="Permalink to this definition">¶</a></dt>
<dd><p>For convenience, a dictionary-like object that searches <tt class="docutils literal"><span class="pre">POST</span></tt> first,
then <tt class="docutils literal"><span class="pre">GET</span></tt>. Inspired by PHP's <tt class="docutils literal"><span class="pre">$_REQUEST</span></tt>.</p>
<p>For example, if <tt class="docutils literal"><span class="pre">GET</span> <span class="pre">=</span> <span class="pre">{"name":</span> <span class="pre">"john"}</span></tt> and <tt class="docutils literal"><span class="pre">POST</span> <span class="pre">=</span> <span class="pre">{"age":</span> <span class="pre">'34'}</span></tt>,
<tt class="docutils literal"><span class="pre">REQUEST["name"]</span></tt> would be <tt class="docutils literal"><span class="pre">"john"</span></tt>, and <tt class="docutils literal"><span class="pre">REQUEST["age"]</span></tt> would be
<tt class="docutils literal"><span class="pre">"34"</span></tt>.</p>
<p>It's strongly suggested that you use <tt class="docutils literal"><span class="pre">GET</span></tt> and <tt class="docutils literal"><span class="pre">POST</span></tt> instead of
<tt class="docutils literal"><span class="pre">REQUEST</span></tt>, because the former are more explicit.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.http.HttpRequest.COOKIES">
<!--[django.http.HttpRequest.COOKIES]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">COOKIES</tt><a class="headerlink" href="#django.http.HttpRequest.COOKIES" title="Permalink to this definition">¶</a></dt>
<dd>A standard Python dictionary containing all cookies. Keys and values are
strings.</dd></dl>

<dl class="attribute">
<dt id="django.http.HttpRequest.FILES">
<!--[django.http.HttpRequest.FILES]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">FILES</tt><a class="headerlink" href="#django.http.HttpRequest.FILES" title="Permalink to this definition">¶</a></dt>
<dd><p>A dictionary-like object containing all uploaded files. Each key in
<tt class="docutils literal"><span class="pre">FILES</span></tt> is the <tt class="docutils literal"><span class="pre">name</span></tt> from the <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type="file"</span> <span class="pre">name=""</span> <span class="pre">/&gt;</span></tt>. Each
value in <tt class="docutils literal"><span class="pre">FILES</span></tt> is an <tt class="docutils literal"><span class="pre">UploadedFile</span></tt> object containing the following
attributes:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">read(num_bytes=None)</span></tt> -- Read a number of bytes from the file.</li>
<li><tt class="docutils literal"><span class="pre">name</span></tt> -- The name of the uploaded file.</li>
<li><tt class="docutils literal"><span class="pre">size</span></tt> -- The size, in bytes, of the uploaded file.</li>
<li><tt class="docutils literal"><span class="pre">chunks(chunk_size=None)</span></tt> -- A generator that yields sequential
chunks of data.</li>
</ul>
<p>See <a class="reference external" href="http://docs.djangoproject.com/en/1.0/topics/files/#topics-files"><em>Managing files</em></a> for more information.</p>
<p>Note that <tt class="docutils literal"><span class="pre">FILES</span></tt> will only contain data if the request method was POST
and the <tt class="docutils literal"><span class="pre">&lt;form&gt;</span></tt> that posted to the request had
<tt class="docutils literal"><span class="pre">enctype="multipart/form-data"</span></tt>. Otherwise, <tt class="docutils literal"><span class="pre">FILES</span></tt> will be a blank
dictionary-like object.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>In previous versions of Django, <tt class="docutils literal"><span class="pre">request.FILES</span></tt> contained simple <tt class="docutils literal"><span class="pre">dict</span></tt>
objects representing uploaded files. This is no longer true -- files are
represented by <tt class="docutils literal"><span class="pre">UploadedFile</span></tt> objects as described below.</p>
<p>These <tt class="docutils literal"><span class="pre">UploadedFile</span></tt> objects will emulate the old-style <tt class="docutils literal"><span class="pre">dict</span></tt>
interface, but this is deprecated and will be removed in the next release of
Django.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.http.HttpRequest.META">
<!--[django.http.HttpRequest.META]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">META</tt><a class="headerlink" href="#django.http.HttpRequest.META" title="Permalink to this definition">¶</a></dt>
<dd><p>A standard Python dictionary containing all available HTTP headers.
Available headers depend on the client and server, but here are some
examples:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">CONTENT_LENGTH</span></tt></li>
<li><tt class="docutils literal"><span class="pre">CONTENT_TYPE</span></tt></li>
<li><tt class="docutils literal"><span class="pre">HTTP_ACCEPT_ENCODING</span></tt></li>
<li><tt class="docutils literal"><span class="pre">HTTP_ACCEPT_LANGUAGE</span></tt></li>
<li><tt class="docutils literal"><span class="pre">HTTP_HOST</span></tt> -- The HTTP Host header sent by the client.</li>
<li><tt class="docutils literal"><span class="pre">HTTP_REFERER</span></tt> -- The referring page, if any.</li>
<li><tt class="docutils literal"><span class="pre">HTTP_USER_AGENT</span></tt> -- The client's user-agent string.</li>
<li><tt class="docutils literal"><span class="pre">QUERY_STRING</span></tt> -- The query string, as a single (unparsed) string.</li>
<li><tt class="docutils literal"><span class="pre">REMOTE_ADDR</span></tt> -- The IP address of the client.</li>
<li><tt class="docutils literal"><span class="pre">REMOTE_HOST</span></tt> -- The hostname of the client.</li>
<li><tt class="docutils literal"><span class="pre">REQUEST_METHOD</span></tt> -- A string such as <tt class="docutils literal"><span class="pre">"GET"</span></tt> or <tt class="docutils literal"><span class="pre">"POST"</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">SERVER_NAME</span></tt> -- The hostname of the server.</li>
<li><tt class="docutils literal"><span class="pre">SERVER_PORT</span></tt> -- The port of the server.</li>
</ul>
<p>With the exception of <tt class="docutils literal"><span class="pre">CONTENT_LENGTH</span></tt> and <tt class="docutils literal"><span class="pre">CONTENT_TYPE</span></tt>, as given
above, any HTTP headers in the request are converted to <tt class="docutils literal"><span class="pre">META</span></tt> keys by
converting all characters to uppercase, replacing any hyphens with
underscores and adding an <tt class="docutils literal"><span class="pre">HTTP_</span></tt> prefix to the name. So, for example, a
header called <tt class="docutils literal"><span class="pre">X-Bender</span></tt> would be mapped to the <tt class="docutils literal"><span class="pre">META</span></tt> key
<tt class="docutils literal"><span class="pre">HTTP_X_BENDER</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.http.HttpRequest.user">
<!--[django.http.HttpRequest.user]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">user</tt><a class="headerlink" href="#django.http.HttpRequest.user" title="Permalink to this definition">¶</a></dt>
<dd><p>A <tt class="docutils literal"><span class="pre">django.contrib.auth.models.User</span></tt> object representing the currently
logged-in user. If the user isn't currently logged in, <tt class="docutils literal"><span class="pre">user</span></tt> will be set
to an instance of <tt class="docutils literal"><span class="pre">django.contrib.auth.models.AnonymousUser</span></tt>. You
can tell them apart with <tt class="docutils literal"><span class="pre">is_authenticated()</span></tt>, like so:</p>
<div class="highlight-python"><pre>if request.user.is_authenticated():
    # Do something for logged-in users.
else:
    # Do something for anonymous users.</pre>
</div>
<p><tt class="docutils literal"><span class="pre">user</span></tt> is only available if your Django installation has the
<tt class="docutils literal"><span class="pre">AuthenticationMiddleware</span></tt> activated. For more, see
<a class="reference external" href="http://docs.djangoproject.com/en/1.0/topics/auth/#topics-auth"><em>User authentication in Django</em></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.http.HttpRequest.session">
<!--[django.http.HttpRequest.session]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">session</tt><a class="headerlink" href="#django.http.HttpRequest.session" title="Permalink to this definition">¶</a></dt>
<dd>A readable-and-writable, dictionary-like object that represents the current
session. This is only available if your Django installation has session
support activated. See the <a class="reference external" href="http://docs.djangoproject.com/en/1.0/topics/http/sessions/#topics-http-sessions"><em>session documentation</em></a> for full details.</dd></dl>

<dl class="attribute">
<dt id="django.http.HttpRequest.raw_post_data">
<!--[django.http.HttpRequest.raw_post_data]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">raw_post_data</tt><a class="headerlink" href="#django.http.HttpRequest.raw_post_data" title="Permalink to this definition">¶</a></dt>
<dd>The raw HTTP POST data. This is only useful for advanced processing. Use
<tt class="docutils literal"><span class="pre">POST</span></tt> instead.</dd></dl>

<dl class="attribute">
<dt id="django.http.HttpRequest.urlconf">
<!--[django.http.HttpRequest.urlconf]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">urlconf</tt><a class="headerlink" href="#django.http.HttpRequest.urlconf" title="Permalink to this definition">¶</a></dt>
<dd>Not defined by Django itself, but will be read if other code (e.g., a custom
middleware class) sets it. When present, this will be used as the root
URLconf for the current request, overriding the <tt class="docutils literal"><span class="pre">ROOT_URLCONF</span></tt> setting.
See <a class="reference external" href="http://docs.djangoproject.com/en/1.0/topics/http/urls/#how-django-processes-a-request"><em>How Django processes a request</em></a> for details.</dd></dl>

</div>
<div class="section" id="s-methods">
<span id="methods"></span><h3>Methods<a class="headerlink" href="#methods" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.http.HttpRequest.get_host">
<!--[django.http.HttpRequest.get_host]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">get_host</tt>()<a class="headerlink" href="#django.http.HttpRequest.get_host" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>Returns the originating host of the request using information from the
<tt class="docutils literal"><span class="pre">HTTP_X_FORWARDED_HOST</span></tt> and <tt class="docutils literal"><span class="pre">HTTP_HOST</span></tt> headers (in that order). If
they don't provide a value, the method uses a combination of
<tt class="docutils literal"><span class="pre">SERVER_NAME</span></tt> and <tt class="docutils literal"><span class="pre">SERVER_PORT</span></tt> as detailed in <a class="reference external" href="http://www.python.org/dev/peps/pep-0333/">PEP 333</a>.</p>
<p>Example: <tt class="docutils literal"><span class="pre">"127.0.0.1:8000"</span></tt></p>
</dd></dl>

<dl class="method">
<dt id="django.http.HttpRequest.get_full_path">
<!--[django.http.HttpRequest.get_full_path]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">get_full_path</tt>()<a class="headerlink" href="#django.http.HttpRequest.get_full_path" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the <tt class="docutils literal"><span class="pre">path</span></tt>, plus an appended query string, if applicable.</p>
<p>Example: <tt class="docutils literal"><span class="pre">"/music/bands/the_beatles/?print=true"</span></tt></p>
</dd></dl>

<dl class="method">
<dt id="django.http.HttpRequest.build_absolute_uri">
<!--[django.http.HttpRequest.build_absolute_uri]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">build_absolute_uri</tt>(<em>location</em>)<a class="headerlink" href="#django.http.HttpRequest.build_absolute_uri" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>Returns the absolute URI form of <tt class="docutils literal"><span class="pre">location</span></tt>. If no location is provided,
the location will be set to <tt class="docutils literal"><span class="pre">request.get_full_path()</span></tt>.</p>
<p>If the location is already an absolute URI, it will not be altered.
Otherwise the absolute URI is built using the server variables available in
this request.</p>
<p>Example: <tt class="docutils literal"><span class="pre">"http://example.com/music/bands/the_beatles/?print=true"</span></tt></p>
</dd></dl>

<dl class="method">
<dt id="django.http.HttpRequest.is_secure">
<!--[django.http.HttpRequest.is_secure]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">is_secure</tt>()<a class="headerlink" href="#django.http.HttpRequest.is_secure" title="Permalink to this definition">¶</a></dt>
<dd>Returns <tt class="xref docutils literal"><span class="pre">True</span></tt> if the request is secure; that is, if it was made with
HTTPS.</dd></dl>

<dl class="method">
<dt id="django.http.HttpRequest.is_ajax">
<!--[django.http.HttpRequest.is_ajax]--><tt class="descclassname">HttpRequest.</tt><tt class="descname">is_ajax</tt>()<a class="headerlink" href="#django.http.HttpRequest.is_ajax" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>Returns <tt class="xref docutils literal"><span class="pre">True</span></tt> if the request was made via an <tt class="docutils literal"><span class="pre">XMLHttpRequest</span></tt>, by
checking the <tt class="docutils literal"><span class="pre">HTTP_X_REQUESTED_WITH</span></tt> header for the string
<tt class="docutils literal"><span class="pre">'XMLHttpRequest'</span></tt>. The following major JavaScript libraries all send this
header:</p>
<ul class="simple">
<li>jQuery</li>
<li>Dojo</li>
<li>MochiKit</li>
<li>MooTools</li>
<li>Prototype</li>
<li>YUI</li>
</ul>
<p>If you write your own XMLHttpRequest call (on the browser side), you'll
have to set this header manually if you want <tt class="docutils literal"><span class="pre">is_ajax()</span></tt> to work.</p>
</dd></dl>

</div>
<div class="section" id="s-querydict-objects">
<span id="querydict-objects"></span><h3>QueryDict objects<a class="headerlink" href="#querydict-objects" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.http.QueryDict">
<!--[django.http.QueryDict]-->class <tt class="descname">QueryDict</tt><a class="headerlink" href="#django.http.QueryDict" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>In an <a title="django.http.HttpRequest" class="reference internal" href="#django.http.HttpRequest"><tt class="xref docutils literal"><span class="pre">HttpRequest</span></tt></a> object, the <tt class="docutils literal"><span class="pre">GET</span></tt> and <tt class="docutils literal"><span class="pre">POST</span></tt> attributes are instances
of <tt class="docutils literal"><span class="pre">django.http.QueryDict</span></tt>. <a title="django.http.QueryDict" class="reference internal" href="#django.http.QueryDict"><tt class="xref docutils literal"><span class="pre">QueryDict</span></tt></a> is a dictionary-like
class customized to deal with multiple values for the same key. This is
necessary because some HTML form elements, notably
<tt class="docutils literal"><span class="pre">&lt;select</span> <span class="pre">multiple="multiple"&gt;</span></tt>, pass multiple values for the same key.</p>
<p><tt class="docutils literal"><span class="pre">QueryDict</span></tt> instances are immutable, unless you create a <tt class="docutils literal"><span class="pre">copy()</span></tt> of them.
That means you can't change attributes of <tt class="docutils literal"><span class="pre">request.POST</span></tt> and <tt class="docutils literal"><span class="pre">request.GET</span></tt>
directly.</p>
</div>
<div class="section" id="s-id1">
<span id="id1"></span><h3>Methods<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<p><a title="django.http.QueryDict" class="reference internal" href="#django.http.QueryDict"><tt class="xref docutils literal"><span class="pre">QueryDict</span></tt></a> implements all the standard dictionary methods, because it's
a subclass of dictionary. Exceptions are outlined here:</p>
<dl class="method">
<dt id="django.http.QueryDict.__getitem__">
<!--[django.http.QueryDict.__getitem__]--><tt class="descclassname">QueryDict.</tt><tt class="descname">__getitem__</tt>(<em>key</em>)<a class="headerlink" href="#django.http.QueryDict.__getitem__" title="Permalink to this definition">¶</a></dt>
<dd>Returns the value for the given key. If the key has more than one value,
<tt class="docutils literal"><span class="pre">__getitem__()</span></tt> returns the last value. Raises
<tt class="docutils literal"><span class="pre">django.utils.datastructure.MultiValueDictKeyError</span></tt> if the key does not
exist. (This is a subclass of Python's standard <tt class="docutils literal"><span class="pre">KeyError</span></tt>, so you can
stick to catching <tt class="docutils literal"><span class="pre">KeyError</span></tt>.)</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.__setitem__">
<!--[django.http.QueryDict.__setitem__]--><tt class="descclassname">QueryDict.</tt><tt class="descname">__setitem__</tt>(<em>key</em>, <em>value</em>)<a class="headerlink" href="#django.http.QueryDict.__setitem__" title="Permalink to this definition">¶</a></dt>
<dd>Sets the given key to <tt class="docutils literal"><span class="pre">[value]</span></tt> (a Python list whose single element is
<tt class="docutils literal"><span class="pre">value</span></tt>). Note that this, as other dictionary functions that have side
effects, can only be called on a mutable <tt class="docutils literal"><span class="pre">QueryDict</span></tt> (one that was created
via <tt class="docutils literal"><span class="pre">copy()</span></tt>).</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.__contains__">
<!--[django.http.QueryDict.__contains__]--><tt class="descclassname">QueryDict.</tt><tt class="descname">__contains__</tt>(<em>key</em>)<a class="headerlink" href="#django.http.QueryDict.__contains__" title="Permalink to this definition">¶</a></dt>
<dd>Returns <tt class="xref docutils literal"><span class="pre">True</span></tt> if the given key is set. This lets you do, e.g., <tt class="docutils literal"><span class="pre">if</span> <span class="pre">"foo"</span>
<span class="pre">in</span> <span class="pre">request.GET</span></tt>.</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.get">
<!--[django.http.QueryDict.get]--><tt class="descclassname">QueryDict.</tt><tt class="descname">get</tt>(<em>key</em>, <em>default</em>)<a class="headerlink" href="#django.http.QueryDict.get" title="Permalink to this definition">¶</a></dt>
<dd>Uses the same logic as <tt class="docutils literal"><span class="pre">__getitem__()</span></tt> above, with a hook for returning a
default value if the key doesn't exist.</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.setdefault">
<!--[django.http.QueryDict.setdefault]--><tt class="descclassname">QueryDict.</tt><tt class="descname">setdefault</tt>(<em>key</em>, <em>default</em>)<a class="headerlink" href="#django.http.QueryDict.setdefault" title="Permalink to this definition">¶</a></dt>
<dd>Just like the standard dictionary <tt class="docutils literal"><span class="pre">setdefault()</span></tt> method, except it uses
<tt class="docutils literal"><span class="pre">__setitem__</span></tt> internally.</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.update">
<!--[django.http.QueryDict.update]--><tt class="descclassname">QueryDict.</tt><tt class="descname">update</tt>(<em>other_dict</em>)<a class="headerlink" href="#django.http.QueryDict.update" title="Permalink to this definition">¶</a></dt>
<dd><p>Takes either a <tt class="docutils literal"><span class="pre">QueryDict</span></tt> or standard dictionary. Just like the standard
dictionary <tt class="docutils literal"><span class="pre">update()</span></tt> method, except it <em>appends</em> to the current
dictionary items rather than replacing them. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">q</span> <span class="o">=</span> <span class="n">QueryDict</span><span class="p">(</span><span class="s">'a=1'</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span> <span class="o">=</span> <span class="n">q</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> <span class="c"># to make it mutable</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="s">'a'</span><span class="p">:</span> <span class="s">'2'</span><span class="p">})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span><span class="o">.</span><span class="n">getlist</span><span class="p">(</span><span class="s">'a'</span><span class="p">)</span>
<span class="go">['1', '2']</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span><span class="p">[</span><span class="s">'a'</span><span class="p">]</span> <span class="c"># returns the last</span>
<span class="go">['2']</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.items">
<!--[django.http.QueryDict.items]--><tt class="descclassname">QueryDict.</tt><tt class="descname">items</tt>()<a class="headerlink" href="#django.http.QueryDict.items" title="Permalink to this definition">¶</a></dt>
<dd><p>Just like the standard dictionary <tt class="docutils literal"><span class="pre">items()</span></tt> method, except this uses the
same last-value logic as <tt class="docutils literal"><span class="pre">__getitem()__</span></tt>. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">q</span> <span class="o">=</span> <span class="n">QueryDict</span><span class="p">(</span><span class="s">'a=1&amp;a=2&amp;a=3'</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
<span class="go">[('a', '3')]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.iteritems">
<!--[django.http.QueryDict.iteritems]--><tt class="descclassname">QueryDict.</tt><tt class="descname">iteritems</tt>()<a class="headerlink" href="#django.http.QueryDict.iteritems" title="Permalink to this definition">¶</a></dt>
<dd>Just like the standard dictionary <tt class="docutils literal"><span class="pre">iteritems()</span></tt> method. Like
<a title="django.http.QueryDict.items" class="reference internal" href="#django.http.QueryDict.items"><tt class="xref docutils literal"><span class="pre">QueryDict.items()</span></tt></a> this uses the same last-value logic as
<tt class="xref docutils literal"><span class="pre">QueryDict.__getitem()__()</span></tt>.</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.iterlists">
<!--[django.http.QueryDict.iterlists]--><tt class="descclassname">QueryDict.</tt><tt class="descname">iterlists</tt>()<a class="headerlink" href="#django.http.QueryDict.iterlists" title="Permalink to this definition">¶</a></dt>
<dd>Like <a title="django.http.QueryDict.iteritems" class="reference internal" href="#django.http.QueryDict.iteritems"><tt class="xref docutils literal"><span class="pre">QueryDict.iteritems()</span></tt></a> except it includes all values, as a list,
for each member of the dictionary.</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.values">
<!--[django.http.QueryDict.values]--><tt class="descclassname">QueryDict.</tt><tt class="descname">values</tt>()<a class="headerlink" href="#django.http.QueryDict.values" title="Permalink to this definition">¶</a></dt>
<dd><p>Just like the standard dictionary <tt class="docutils literal"><span class="pre">values()</span></tt> method, except this uses the
same last-value logic as <tt class="docutils literal"><span class="pre">__getitem()__</span></tt>. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">q</span> <span class="o">=</span> <span class="n">QueryDict</span><span class="p">(</span><span class="s">'a=1&amp;a=2&amp;a=3'</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span><span class="o">.</span><span class="n">values</span><span class="p">()</span>
<span class="go">['3']</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.itervalues">
<!--[django.http.QueryDict.itervalues]--><tt class="descclassname">QueryDict.</tt><tt class="descname">itervalues</tt>()<a class="headerlink" href="#django.http.QueryDict.itervalues" title="Permalink to this definition">¶</a></dt>
<dd>Just like <a title="django.http.QueryDict.values" class="reference internal" href="#django.http.QueryDict.values"><tt class="xref docutils literal"><span class="pre">QueryDict.values()</span></tt></a>, except an iterator.</dd></dl>

<p>In addition, <tt class="docutils literal"><span class="pre">QueryDict</span></tt> has the following methods:</p>
<dl class="method">
<dt id="django.http.QueryDict.copy">
<!--[django.http.QueryDict.copy]--><tt class="descclassname">QueryDict.</tt><tt class="descname">copy</tt>()<a class="headerlink" href="#django.http.QueryDict.copy" title="Permalink to this definition">¶</a></dt>
<dd>Returns a copy of the object, using <tt class="docutils literal"><span class="pre">copy.deepcopy()</span></tt> from the Python
standard library. The copy will be mutable -- that is, you can change its
values.</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.getlist">
<!--[django.http.QueryDict.getlist]--><tt class="descclassname">QueryDict.</tt><tt class="descname">getlist</tt>(<em>key</em>)<a class="headerlink" href="#django.http.QueryDict.getlist" title="Permalink to this definition">¶</a></dt>
<dd>Returns the data with the requested key, as a Python list. Returns an
empty list if the key doesn't exist. It's guaranteed to return a list of
some sort.</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.setlist">
<!--[django.http.QueryDict.setlist]--><tt class="descclassname">QueryDict.</tt><tt class="descname">setlist</tt>(<em>key</em>, <em>list_</em>)<a class="headerlink" href="#django.http.QueryDict.setlist" title="Permalink to this definition">¶</a></dt>
<dd>Sets the given key to <tt class="docutils literal"><span class="pre">list_</span></tt> (unlike <tt class="docutils literal"><span class="pre">__setitem__()</span></tt>).</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.appendlist">
<!--[django.http.QueryDict.appendlist]--><tt class="descclassname">QueryDict.</tt><tt class="descname">appendlist</tt>(<em>key</em>, <em>item</em>)<a class="headerlink" href="#django.http.QueryDict.appendlist" title="Permalink to this definition">¶</a></dt>
<dd>Appends an item to the internal list associated with key.</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.setlistdefault">
<!--[django.http.QueryDict.setlistdefault]--><tt class="descclassname">QueryDict.</tt><tt class="descname">setlistdefault</tt>(<em>key</em>, <em>default_list</em>)<a class="headerlink" href="#django.http.QueryDict.setlistdefault" title="Permalink to this definition">¶</a></dt>
<dd>Just like <tt class="docutils literal"><span class="pre">setdefault</span></tt>, except it takes a list of values instead of a
single value.</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.lists">
<!--[django.http.QueryDict.lists]--><tt class="descclassname">QueryDict.</tt><tt class="descname">lists</tt>()<a class="headerlink" href="#django.http.QueryDict.lists" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a title="django.http.QueryDict.items" class="reference internal" href="#django.http.QueryDict.items"><tt class="xref docutils literal"><span class="pre">items()</span></tt></a>, except it includes all values, as a list, for each
member of the dictionary. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">q</span> <span class="o">=</span> <span class="n">QueryDict</span><span class="p">(</span><span class="s">'a=1&amp;a=2&amp;a=3'</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span><span class="o">.</span><span class="n">lists</span><span class="p">()</span>
<span class="go">[('a', ['1', '2', '3'])]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.http.QueryDict.urlencode">
<!--[django.http.QueryDict.urlencode]--><tt class="descclassname">QueryDict.</tt><tt class="descname">urlencode</tt>()<a class="headerlink" href="#django.http.QueryDict.urlencode" title="Permalink to this definition">¶</a></dt>
<dd>Returns a string of the data in query-string format.
Example: <tt class="docutils literal"><span class="pre">"a=2&amp;b=3&amp;b=5"</span></tt>.</dd></dl>

</div>
</div>
<div class="section" id="s-httpresponse-objects">
<span id="httpresponse-objects"></span><h2>HttpResponse objects<a class="headerlink" href="#httpresponse-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.http.HttpResponse">
<!--[django.http.HttpResponse]-->class <tt class="descname">HttpResponse</tt><a class="headerlink" href="#django.http.HttpResponse" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>In contrast to <a title="django.http.HttpRequest" class="reference internal" href="#django.http.HttpRequest"><tt class="xref docutils literal"><span class="pre">HttpRequest</span></tt></a> objects, which are created automatically by
Django, <a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a> objects are your responsibility. Each view you
write is responsible for instantiating, populating and returning an
<a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a>.</p>
<p>The <a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a> class lives in the <tt class="docutils literal"><span class="pre">django.http</span></tt> module.</p>
<div class="section" id="s-usage">
<span id="usage"></span><h3>Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h3>
<div class="section" id="s-passing-strings">
<span id="passing-strings"></span><h4>Passing strings<a class="headerlink" href="#passing-strings" title="Permalink to this headline">¶</a></h4>
<p>Typical usage is to pass the contents of the page, as a string, to the
<a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a> constructor:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span> <span class="o">=</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">"Here's the text of the Web page."</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span> <span class="o">=</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">"Text only, please."</span><span class="p">,</span> <span class="n">mimetype</span><span class="o">=</span><span class="s">"text/plain"</span><span class="p">)</span>
</pre></div>
</div>
<p>But if you want to add content incrementally, you can use <tt class="docutils literal"><span class="pre">response</span></tt> as a
file-like object:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span> <span class="o">=</span> <span class="n">HttpResponse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"&lt;p&gt;Here's the text of the Web page.&lt;/p&gt;"</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"&lt;p&gt;Here's another paragraph.&lt;/p&gt;"</span><span class="p">)</span>
</pre></div>
</div>
<p>You can add and delete headers using dictionary syntax:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span> <span class="o">=</span> <span class="n">HttpResponse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="p">[</span><span class="s">'X-DJANGO'</span><span class="p">]</span> <span class="o">=</span> <span class="s">"It's the best."</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">response</span><span class="p">[</span><span class="s">'X-PHP'</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="p">[</span><span class="s">'X-DJANGO'</span><span class="p">]</span>
<span class="go">"It's the best."</span>
</pre></div>
</div>
<p>Note that <tt class="docutils literal"><span class="pre">del</span></tt> doesn't raise <tt class="docutils literal"><span class="pre">KeyError</span></tt> if the header doesn't exist.</p>
</div>
<div class="section" id="s-passing-iterators">
<span id="passing-iterators"></span><h4>Passing iterators<a class="headerlink" href="#passing-iterators" title="Permalink to this headline">¶</a></h4>
<p>Finally, you can pass <tt class="docutils literal"><span class="pre">HttpResponse</span></tt> an iterator rather than passing it
hard-coded strings. If you use this technique, follow these guidelines:</p>
<ul class="simple">
<li>The iterator should return strings.</li>
<li>If an <a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a> has been initialized with an iterator as its
content, you can't use the class:<cite>HttpResponse</cite> instance as a file-like
object. Doing so will raise <tt class="docutils literal"><span class="pre">Exception</span></tt>.</li>
</ul>
</div>
<div class="section" id="s-setting-headers">
<span id="setting-headers"></span><h4>Setting headers<a class="headerlink" href="#setting-headers" title="Permalink to this headline">¶</a></h4>
<p>To set a header in your response, just treat it like a dictionary:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span> <span class="o">=</span> <span class="n">HttpResponse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="p">[</span><span class="s">'Pragma'</span><span class="p">]</span> <span class="o">=</span> <span class="s">'no-cache'</span>
</pre></div>
</div>
</div>
<div class="section" id="s-telling-the-browser-to-treat-the-response-as-a-file-attachment">
<span id="telling-the-browser-to-treat-the-response-as-a-file-attachment"></span><h4>Telling the browser to treat the response as a file attachment<a class="headerlink" href="#telling-the-browser-to-treat-the-response-as-a-file-attachment" title="Permalink to this headline">¶</a></h4>
<p>To tell the browser to treat the response as a file attachment, use the
<tt class="docutils literal"><span class="pre">mimetype</span></tt> argument and set the <tt class="docutils literal"><span class="pre">Content-Disposition</span></tt> header. For example,
this is how you might return a Microsoft Excel spreadsheet:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span> <span class="o">=</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">my_data</span><span class="p">,</span> <span class="n">mimetype</span><span class="o">=</span><span class="s">'application/vnd.ms-excel'</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span><span class="p">[</span><span class="s">'Content-Disposition'</span><span class="p">]</span> <span class="o">=</span> <span class="s">'attachment; filename=foo.xls'</span>
</pre></div>
</div>
<p>There's nothing Django-specific about the <tt class="docutils literal"><span class="pre">Content-Disposition</span></tt> header, but
it's easy to forget the syntax, so we've included it here.</p>
</div>
</div>
<div class="section" id="s-id2">
<span id="id2"></span><h3>Attributes<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.http.HttpResponse.content">
<!--[django.http.HttpResponse.content]--><tt class="descclassname">HttpResponse.</tt><tt class="descname">content</tt><a class="headerlink" href="#django.http.HttpResponse.content" title="Permalink to this definition">¶</a></dt>
<dd>A normal Python string representing the content, encoded from a Unicode
object if necessary.</dd></dl>

</div>
<div class="section" id="s-id3">
<span id="id3"></span><h3>Methods<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.http.HttpResponse.__init__">
<!--[django.http.HttpResponse.__init__]--><tt class="descclassname">HttpResponse.</tt><tt class="descname">__init__</tt>(<em>content=''</em>, <em>mimetype=None</em>, <em>status=200</em>, <em>content_type=DEFAULT_CONTENT_TYPE</em>)<a class="headerlink" href="#django.http.HttpResponse.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Instantiates an <tt class="docutils literal"><span class="pre">HttpResponse</span></tt> object with the given page content (a
string) and MIME type. The <tt class="docutils literal"><span class="pre">DEFAULT_CONTENT_TYPE</span></tt> is <tt class="docutils literal"><span class="pre">'text/html'</span></tt>.</p>
<p><tt class="docutils literal"><span class="pre">content</span></tt> can be an iterator or a string. If it's an iterator, it should
return strings, and those strings will be joined together to form the
content of the response.</p>
<p><tt class="docutils literal"><span class="pre">status</span></tt> is the <a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10">HTTP Status code</a> for the response.</p>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p><tt class="docutils literal"><span class="pre">content_type</span></tt> is an alias for <tt class="docutils literal"><span class="pre">mimetype</span></tt>. Historically, this parameter
was only called <tt class="docutils literal"><span class="pre">mimetype</span></tt>, but since this is actually the value included
in the HTTP <tt class="docutils literal"><span class="pre">Content-Type</span></tt> header, it can also include the character set
encoding, which makes it more than just a MIME type specification.
If <tt class="docutils literal"><span class="pre">mimetype</span></tt> is specified (not <tt class="xref docutils literal"><span class="pre">None</span></tt>), that value is used.
Otherwise, <tt class="docutils literal"><span class="pre">content_type</span></tt> is used. If neither is given, the
<tt class="docutils literal"><span class="pre">DEFAULT_CONTENT_TYPE</span></tt> setting is used.</p>
</dd></dl>

<dl class="method">
<dt id="django.http.HttpResponse.__setitem__">
<!--[django.http.HttpResponse.__setitem__]--><tt class="descclassname">HttpResponse.</tt><tt class="descname">__setitem__</tt>(<em>header</em>, <em>value</em>)<a class="headerlink" href="#django.http.HttpResponse.__setitem__" title="Permalink to this definition">¶</a></dt>
<dd>Sets the given header name to the given value. Both <tt class="docutils literal"><span class="pre">header</span></tt> and
<tt class="docutils literal"><span class="pre">value</span></tt> should be strings.</dd></dl>

<dl class="method">
<dt id="django.http.HttpResponse.__delitem__">
<!--[django.http.HttpResponse.__delitem__]--><tt class="descclassname">HttpResponse.</tt><tt class="descname">__delitem__</tt>(<em>header</em>)<a class="headerlink" href="#django.http.HttpResponse.__delitem__" title="Permalink to this definition">¶</a></dt>
<dd>Deletes the header with the given name. Fails silently if the header
doesn't exist. Case-sensitive.</dd></dl>

<dl class="method">
<dt id="django.http.HttpResponse.__getitem__">
<!--[django.http.HttpResponse.__getitem__]--><tt class="descclassname">HttpResponse.</tt><tt class="descname">__getitem__</tt>(<em>header</em>)<a class="headerlink" href="#django.http.HttpResponse.__getitem__" title="Permalink to this definition">¶</a></dt>
<dd>Returns the value for the given header name. Case-sensitive.</dd></dl>

<dl class="method">
<dt id="django.http.HttpResponse.has_header">
<!--[django.http.HttpResponse.has_header]--><tt class="descclassname">HttpResponse.</tt><tt class="descname">has_header</tt>(<em>header</em>)<a class="headerlink" href="#django.http.HttpResponse.has_header" title="Permalink to this definition">¶</a></dt>
<dd>Returns <tt class="xref docutils literal"><span class="pre">True</span></tt> or <tt class="xref docutils literal"><span class="pre">False</span></tt> based on a case-insensitive check for a
header with the given name.</dd></dl>

<dl class="method">
<dt id="django.http.HttpResponse.set_cookie">
<!--[django.http.HttpResponse.set_cookie]--><tt class="descclassname">HttpResponse.</tt><tt class="descname">set_cookie</tt>(<em>key</em>, <em>value=''</em>, <em>max_age=None</em>, <em>expires=None</em>, <em>path='/'</em>, <em>domain=None</em>, <em>secure=None</em>)<a class="headerlink" href="#django.http.HttpResponse.set_cookie" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets a cookie. The parameters are the same as in the <a class="reference external" href="http://docs.python.org/library/cookie.html#Cookie.Morsel">cookie Morsel</a>
object in the Python standard library.</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">max_age</span></tt> should be a number of seconds, or <tt class="xref docutils literal"><span class="pre">None</span></tt> (default) if
the cookie should last only as long as the client's browser session.</li>
<li><tt class="docutils literal"><span class="pre">expires</span></tt> should be a string in the format
<tt class="docutils literal"><span class="pre">"Wdy,</span> <span class="pre">DD-Mon-YY</span> <span class="pre">HH:MM:SS</span> <span class="pre">GMT"</span></tt>.</li>
<li>Use <tt class="docutils literal"><span class="pre">domain</span></tt> if you want to set a cross-domain cookie. For example,
<tt class="docutils literal"><span class="pre">domain=".lawrence.com"</span></tt> will set a cookie that is readable by
the domains www.lawrence.com, blogs.lawrence.com and
calendars.lawrence.com. Otherwise, a cookie will only be readable by
the domain that set it.</li>
</ul>
</dd></dl>

<dl class="method">
<dt id="django.http.HttpResponse.delete_cookie">
<!--[django.http.HttpResponse.delete_cookie]--><tt class="descclassname">HttpResponse.</tt><tt class="descname">delete_cookie</tt>(<em>key</em>, <em>path='/'</em>, <em>domain=None</em>)<a class="headerlink" href="#django.http.HttpResponse.delete_cookie" title="Permalink to this definition">¶</a></dt>
<dd><p>Deletes the cookie with the given key. Fails silently if the key doesn't
exist.</p>
<p>Due to the way cookies work, <tt class="docutils literal"><span class="pre">path</span></tt> and <tt class="docutils literal"><span class="pre">domain</span></tt> should be the same
values you used in <tt class="docutils literal"><span class="pre">set_cookie()</span></tt> -- otherwise the cookie may not be
deleted.</p>
</dd></dl>

<dl class="method">
<dt id="django.http.HttpResponse.write">
<!--[django.http.HttpResponse.write]--><tt class="descclassname">HttpResponse.</tt><tt class="descname">write</tt>(<em>content</em>)<a class="headerlink" href="#django.http.HttpResponse.write" title="Permalink to this definition">¶</a></dt>
<dd>This method makes an <a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a> instance a file-like object.</dd></dl>

<dl class="method">
<dt id="django.http.HttpResponse.flush">
<!--[django.http.HttpResponse.flush]--><tt class="descclassname">HttpResponse.</tt><tt class="descname">flush</tt>()<a class="headerlink" href="#django.http.HttpResponse.flush" title="Permalink to this definition">¶</a></dt>
<dd>This method makes an <a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a> instance a file-like object.</dd></dl>

<dl class="method">
<dt id="django.http.HttpResponse.tell">
<!--[django.http.HttpResponse.tell]--><tt class="descclassname">HttpResponse.</tt><tt class="descname">tell</tt>()<a class="headerlink" href="#django.http.HttpResponse.tell" title="Permalink to this definition">¶</a></dt>
<dd>This method makes an <a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a> instance a file-like object.</dd></dl>

</div>
<div class="section" id="s-httpresponse-subclasses">
<span id="s-ref-httpresponse-subclasses"></span><span id="httpresponse-subclasses"></span><span id="ref-httpresponse-subclasses"></span><h3>HttpResponse subclasses<a class="headerlink" href="#httpresponse-subclasses" title="Permalink to this headline">¶</a></h3>
<p>Django includes a number of <tt class="docutils literal"><span class="pre">HttpResponse</span></tt> subclasses that handle different
types of HTTP responses. Like <tt class="docutils literal"><span class="pre">HttpResponse</span></tt>, these subclasses live in
<tt class="xref docutils literal"><span class="pre">django.http</span></tt>.</p>
<dl class="class">
<dt id="django.http.HttpResponseRedirect">
<!--[django.http.HttpResponseRedirect]-->class <tt class="descname">HttpResponseRedirect</tt><a class="headerlink" href="#django.http.HttpResponseRedirect" title="Permalink to this definition">¶</a></dt>
<dd>The constructor takes a single argument -- the path to redirect to. This
can be a fully qualified URL (e.g. <tt class="docutils literal"><span class="pre">'http://www.yahoo.com/search/'</span></tt>) or an
absolute URL with no domain (e.g. <tt class="docutils literal"><span class="pre">'/search/'</span></tt>). Note that this returns
an HTTP status code 302.</dd></dl>

<dl class="class">
<dt id="django.http.HttpResponsePermanentRedirect">
<!--[django.http.HttpResponsePermanentRedirect]-->class <tt class="descname">HttpResponsePermanentRedirect</tt><a class="headerlink" href="#django.http.HttpResponsePermanentRedirect" title="Permalink to this definition">¶</a></dt>
<dd>Like <a title="django.http.HttpResponseRedirect" class="reference internal" href="#django.http.HttpResponseRedirect"><tt class="xref docutils literal"><span class="pre">HttpResponseRedirect</span></tt></a>, but it returns a permanent redirect
(HTTP status code 301) instead of a "found" redirect (status code 302).</dd></dl>

<dl class="class">
<dt id="django.http.HttpResponseNotModified">
<!--[django.http.HttpResponseNotModified]-->class <tt class="descname">HttpResponseNotModified</tt><a class="headerlink" href="#django.http.HttpResponseNotModified" title="Permalink to this definition">¶</a></dt>
<dd>The constructor doesn't take any arguments. Use this to designate that a
page hasn't been modified since the user's last request (status code 304).</dd></dl>

<dl class="class">
<dt id="django.http.HttpResponseBadRequest">
<!--[django.http.HttpResponseBadRequest]-->class <tt class="descname">HttpResponseBadRequest</tt><a class="headerlink" href="#django.http.HttpResponseBadRequest" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>Acts just like <a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a> but uses a 400 status code.</p>
</dd></dl>

<dl class="class">
<dt id="django.http.HttpResponseNotFound">
<!--[django.http.HttpResponseNotFound]-->class <tt class="descname">HttpResponseNotFound</tt><a class="headerlink" href="#django.http.HttpResponseNotFound" title="Permalink to this definition">¶</a></dt>
<dd>Acts just like <a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a> but uses a 404 status code.</dd></dl>

<dl class="class">
<dt id="django.http.HttpResponseForbidden">
<!--[django.http.HttpResponseForbidden]-->class <tt class="descname">HttpResponseForbidden</tt><a class="headerlink" href="#django.http.HttpResponseForbidden" title="Permalink to this definition">¶</a></dt>
<dd>Acts just like <a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a> but uses a 403 status code.</dd></dl>

<dl class="class">
<dt id="django.http.HttpResponseNotAllowed">
<!--[django.http.HttpResponseNotAllowed]-->class <tt class="descname">HttpResponseNotAllowed</tt><a class="headerlink" href="#django.http.HttpResponseNotAllowed" title="Permalink to this definition">¶</a></dt>
<dd>Like <a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a>, but uses a 405 status code. Takes a single,
required argument: a list of permitted methods (e.g. <tt class="docutils literal"><span class="pre">['GET',</span> <span class="pre">'POST']</span></tt>).</dd></dl>

<dl class="class">
<dt id="django.http.HttpResponseGone">
<!--[django.http.HttpResponseGone]-->class <tt class="descname">HttpResponseGone</tt><a class="headerlink" href="#django.http.HttpResponseGone" title="Permalink to this definition">¶</a></dt>
<dd>Acts just like <a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a> but uses a 410 status code.</dd></dl>

<dl class="class">
<dt id="django.http.HttpResponseServerError">
<!--[django.http.HttpResponseServerError]-->class <tt class="descname">HttpResponseServerError</tt><a class="headerlink" href="#django.http.HttpResponseServerError" title="Permalink to this definition">¶</a></dt>
<dd>Acts just like <a title="django.http.HttpResponse" class="reference internal" href="#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a> but uses a 500 status code.</dd></dl>

</div>
</div>
</div>



<div id="content-secondary">
  <h2 id="comments">Questions/Feedback</h2>
  <p>Having trouble? We'd like to help!</p>
  <ul>
    <li>
      Try the <a href="http://docs.djangoproject.com/en/dev/faq/">FAQ</a> — it's got answers to many common
      questions.
    </li>
    <li>
      Search for information in the <a href="http://groups.google.com/group/django-users/">archives of the
      django-users mailing list</a>, or <a href="http://groups.google.com/group/django-users/">post a question</a>.
    </li>
    <li>
      Ask a question in the <a href="irc://irc.freenode.net/">#django IRC
      channel</a>, or search the <a href="http://oebfare.com/logger/django/">IRC
      logs</a> to see if its been asked before.
    </li>
    <li>
      If you notice errors with this documentation, please <a href="http://code.djangoproject.com/simpleticket?component=Documentation">
      open a ticket</a> and let us know! Please only use the ticket tracker for
      criticisms and improvements on the docs. For tech support, use the
      resources above.
    </li>
  </ul>
</div>

		</div>
		<!-- END #content-main -->
		<div id="content-related" class="sidebar">
		
  
    <h2>Contents</h2>
    
      <ul>
<li><a class="reference external" href="">Request and response objects</a><ul>
<li><a class="reference external" href="#quick-overview">Quick overview</a></li>
<li><a class="reference external" href="#httprequest-objects">HttpRequest objects</a><ul>
<li><a class="reference external" href="#attributes">Attributes</a></li>
<li><a class="reference external" href="#methods">Methods</a></li>
<li><a class="reference external" href="#querydict-objects">QueryDict objects</a></li>
<li><a class="reference external" href="#id1">Methods</a></li>
</ul>
</li>
<li><a class="reference external" href="#httpresponse-objects">HttpResponse objects</a><ul>
<li><a class="reference external" href="#usage">Usage</a><ul>
<li><a class="reference external" href="#passing-strings">Passing strings</a></li>
<li><a class="reference external" href="#passing-iterators">Passing iterators</a></li>
<li><a class="reference external" href="#setting-headers">Setting headers</a></li>
<li><a class="reference external" href="#telling-the-browser-to-treat-the-response-as-a-file-attachment">Telling the browser to treat the response as a file attachment</a></li>
</ul>
</li>
<li><a class="reference external" href="#id2">Attributes</a></li>
<li><a class="reference external" href="#id3">Methods</a></li>
<li><a class="reference external" href="#httpresponse-subclasses">HttpResponse subclasses</a></li>
</ul>
</li>
</ul>
</li>
</ul>

    
  
  
  
    <h2>Search</h2>
    
    <form action="/en/1.0/search/" id="search" class="search">
  <div>
    <input name="cx" value="009763561546736975936:e88ek0eurf4" type="hidden">
    <input name="cof" value="FORID:11" type="hidden">
    <input name="ie" value="UTF-8" type="hidden">
    <input name="hl" value="" type="hidden">
    <input style="background: rgb(255, 255, 255) url(http://www.google.com/coop/intl/en/images/google_custom_search_watermark.gif) no-repeat scroll left center; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" id="id_search_q" class="query" name="q" type="text">
    <input name="sa" class="submit" value="Search" type="submit">
    <ul>
<li><label for="id_search_as_q_0"><input checked="checked" id="id_search_as_q_0" value="more:dev_docs" name="as_q" type="radio"> Latest</label></li>
<li><label for="id_search_as_q_1"><input id="id_search_as_q_1" value="more:1.0_docs" name="as_q" type="radio"> 1.0</label></li>
<li><label for="id_search_as_q_2"><input id="id_search_as_q_2" value="more:0.96_docs" name="as_q" type="radio"> 0.96</label></li>
<li><label for="id_search_as_q_3"><input id="id_search_as_q_3" value="more:all_docs" name="as_q" type="radio"> All</label></li>
</ul>
  </div>
</form>
<script type="text/javascript" src="Django%20%7C%20Request%20and%20response%20objects%20%7C%20Django%20Documentation_files/brand.html"></script>
  
  
  
    <h2>Browse</h2>
    <ul>
      
        
          <li>Prev: <a href="http://docs.djangoproject.com/en/1.0/ref/models/querysets/">QuerySet API reference</a></li>
        
        
          <li>Next: <a href="http://docs.djangoproject.com/en/1.0/ref/settings/">Available settings</a></li>
        
        <li><a href="http://docs.djangoproject.com/en/1.0/contents/">Table of contents</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/1.0/genindex/">General Index</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/1.0/modindex/">Global Module Index</a></li>
        
      
    </ul>
  
  
  
    <h2>You are here:</h2>
    <ul>
      
        <li>
          <a href="http://docs.djangoproject.com/en/1.0/">Django 1.0 documentation</a>
          
            <ul><li><a href="http://docs.djangoproject.com/en/1.0/ref/">API Reference</a>
          
          <ul><li>Request and response objects</li></ul>
          </li></ul>
        </li>
      
    </ul>
  
  
  
    <h3>Last update:</h3>
    <div>May 23, 2009, 8:15 a.m. (<a href="http://www.timeanddate.com/worldclock/city.html?n=64">CDT</a>)</div>
  

		</div>
		<!-- END #content-related -->

		</div>
		<!-- END #content -->
		<div id="footer">
			<p>© 2005-2009 <a href="http://www.djangoproject.com/foundation/">Django Software Foundation</a> unless otherwise noted. Django is a registered trademark of the Django Software Foundation. 
			Hosting graciously provided by <a href="http://mediatemple.net/">
			<img style="vertical-align: middle; position: relative; top: -1px;" src="Django%20%7C%20Request%20and%20response%20objects%20%7C%20Django%20Documentation_files/mt.png" alt="media temple"></a>
			</p>
		</div>
		<!-- END #footer -->
	</div>
	<!-- END #container -->
	</body></html>